/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function getXMLObject()  //XML OBJECT
{
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
     }
     catch (ex) {
       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;  // Mandatory Statement returning the ajax object created
}

var xmlhttp = new getXMLObject();	//xmlhttp holds the ajax object
var xmlhttpcategory = new getXMLObject();
var xmlhttpmovie = new getXMLObject();

function loginAdmin(){
    
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
    
      
       
   
    if(!username || !password)
        document.getElementById("loginindicator").innerHTML = "Fill out all forms";
    else{
    if(xmlhttp) {
            loadingBarForAdminLogin();
            xmlhttp.open("POST","../AdminLogin",true); //servlet name
            xmlhttp.onreadystatechange  = handleServerResponseForAdminLogin;
            xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttp.send("username="+username+"&password="+password);
    }
    
}
    
}

function handleServerResponseForAdminLogin(){
           
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
            document.getElementById("adminloginpane").innerHTML=xmlhttp.responseText;
           
     }
    else if(xmlhttp.readyState==4 && xmlhttp.status==404){
            pageNotFoundForRegister();
     }
    
}

function loadingBarForLogin(){
    document.getElementById("loadersignin").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='180'  /></div>";
}

function loadingBarForAdminLogin(){
    document.getElementById("adminloginsubmitbutton").innerHTML="<div style='text-align: left;'><img src='../images/loader.gif' alt='Loading' align='left' width='180'  /></div>";
}




function retrieveCategories(){
    
    if(xmlhttpcategory) {
        document.getElementById("categorypaneloader").innerHTML="<div style='text-align: left;'><img src='../images/loader.gif' alt='Loading' align='left' width='180'  /></div>";
        xmlhttpcategory.open("GET","../AdminRetrieveCategories",true); //servlet name
        xmlhttpcategory.onreadystatechange  = handleServerResponseForBrowse;
        xmlhttpcategory.send();
    }
}

function handleServerResponseForBrowse(){
 
     if (xmlhttpcategory.readyState==4 && xmlhttpcategory.status==200)
     {
            document.getElementById("categorytraverse").innerHTML=xmlhttpcategory.responseText;
     }
    else if(xmlhttpcategory.readyState==4 && xmlhttpcategory.status==404){
            xmlhttpcategory.getElementById("categorytraverse").innerHTML="<h3>Page Not Found. Please Try Again</h3>";
     }
}



function gotoEditCategory(id){
    if(xmlhttpcategory) {
        document.getElementById("editcategory"+id).innerHTML="<div style='text-align: left;'><img src='../images/loader.gif' alt='Loading' align='left' width='50px'  /></div>";
        xmlhttpcategory.open("GET","../AdminGotoEditCategory?idCategoryEdit="+id,true); //servlet name
        xmlhttpcategory.onreadystatechange  = handleServerResponseForGotoEditCategory;
        xmlhttpcategory.send();
    }
   
    
    
}

function handleServerResponseForGotoEditCategory(){
     if (xmlhttpcategory.readyState==4 && xmlhttpcategory.status==200)
     {
            document.getElementById("realbody").innerHTML=xmlhttpcategory.responseText;
     }
    else if(xmlhttpcategory.readyState==4 && xmlhttpcategory.status==404){
            xmlhttpcategory.getElementById("realbody").innerHTML="<h3>Page Not Found. Please Try Again</h3>";
     }
}


function categoryEdit(id, pic, name, description){
      
     var categoryname;
     var categorypic;
     var categorydesc;
     
      try{
          categorypic = document.getElementById("specificcategorypic").name;
      }catch(error){
          categorypic = pic;
          
      }
      
       
      try{
          categoryname = document.getElementById("editcategoryname").value;
      }catch(error){
          categoryname = name;
          
      }
      
      try{
          categorydesc = document.getElementById("editcategorydescription").value;
      }catch(error){
          categorydesc = description;
      }
      
      
   
        
    
      if(xmlhttpcategory) {
        document.getElementById("submiteditcategory").innerHTML="<div style='text-align: left;'><img src='../images/loader.gif' alt='Loading' align='left' width='50px'  /></div>";
        xmlhttpcategory.open("GET","../AdminEditCategory?id="+id+"&pic="+categorypic+"&name="+categoryname+"&desc="+categorydesc,true); //servlet name
        xmlhttpcategory.onreadystatechange  = handleServerResponseForEditCategory;
        xmlhttpcategory.send();
      }
   
    
    
}

function handleServerResponseForEditCategory(){
     
     if (xmlhttpcategory.readyState==4 && xmlhttpcategory.status==200)
     {
            document.getElementById("realbody").innerHTML=xmlhttpcategory.responseText;
     }
    else if(xmlhttpcategory.readyState==4 && xmlhttpcategory.status==404){
            xmlhttpcategory.getElementById("realbody").innerHTML="<h3>Page Not Found. Please Try Again</h3>";
     }
}


function handleServerResponseForGotoEditCategory(){
     if (xmlhttpcategory.readyState==4 && xmlhttpcategory.status==200)
     {
            document.getElementById("realbody").innerHTML=xmlhttpcategory.responseText;
     }
    else if(xmlhttpcategory.readyState==4 && xmlhttpcategory.status==404){
            xmlhttpcategory.getElementById("realbody").innerHTML="<h3>Page Not Found. Please Try Again</h3>";
     }
}


function categoryAdd(){
      
     var categoryname = document.getElementById("inputcategoryname").value;
     var categorypic = document.getElementById("inputcategorypic").value.substr(12);
     var categorydesc = document.getElementById("inputcategorydesc").value; 
     
     
     if(!categoryname || !categorydesc){
         document.getElementById("addcategoryerror").innerHTML = "<font color='red'>You must fill the name and description</font>";
     }
     else{
      if(confirm("Are you sure?") == true){   
     
      if(xmlhttpcategory) {
        document.getElementById("addcategoryerror").innerHTML = "";  
        document.getElementById("submitaddcategory").innerHTML="<div style='text-align: left;'><img src='../images/loader.gif' alt='Loading' align='left' width='50px'  /></div>";
        xmlhttpcategory.open("GET","../AdminAddCategory?pic="+categorypic+"&name="+categoryname+"&desc="+categorydesc,true); //servlet name
        xmlhttpcategory.onreadystatechange  = handleServerResponseForEditCategory;
        xmlhttpcategory.send();
      }
      }
   
     }
    
}

function deleteCategory(id){
      
      if(confirm("Are you sure?") == true){   
     
      if(xmlhttpcategory) {
        document.getElementById("deletecategory"+id).innerHTML="<div style='text-align: left;'><img src='../images/loader.gif' alt='Loading' align='left' width='50px'  /></div>";
        xmlhttpcategory.open("GET","../AdminDeleteCategory?id="+id,true); //servlet name
        xmlhttpcategory.onreadystatechange  = handleServerResponseForGotoEditCategory;
        xmlhttpcategory.send();
      }
      
   
     }
    
}

function handleServerResponseForEditCategory(){
   
     if (xmlhttpcategory.readyState==4 && xmlhttpcategory.status==200)
     {
            document.getElementById("realbody").innerHTML=xmlhttpcategory.responseText;
     }
    else if(xmlhttpcategory.readyState==4 && xmlhttpcategory.status==404){
            xmlhttpcategory.getElementById("realbody").innerHTML="<h3>Page Not Found. Please Try Again</h3>";
     }
 }    
     
 function retrieveMovies(){
   
    
    if(xmlhttpcategory) {
        document.getElementById("moviepaneloader").innerHTML="<div style='text-align: left;'><img src='../images/loader.gif' alt='Loading' align='left' width='180'  /></div>";
        xmlhttpcategory.open("GET","../AdminRetrieveMovies",true); //servlet name
        xmlhttpcategory.onreadystatechange  = handleServerResponseForBrowseMovie;
        xmlhttpcategory.send();
    }
}

function handleServerResponseForBrowseMovie(){

     if (xmlhttpcategory.readyState==4 && xmlhttpcategory.status==200)
     {
            document.getElementById("movietraverse").innerHTML=xmlhttpcategory.responseText;
     }
    else if(xmlhttpcategory.readyState==4 && xmlhttpcategory.status==404){
            xmlhttpcategory.getElementById("movietraverse").innerHTML="<h3>Page Not Found. Please Try Again</h3>";
     }
}
     
function gotoEditMovie(id){
    
       
    if(xmlhttpmovie) {
        document.getElementById("editmovie"+id).innerHTML="<div style='text-align: left;'><img src='../images/loader.gif' alt='Loading' align='left' width='50px'  /></div>";
        xmlhttpmovie.open("GET","../AdminGotoEditMovie?idMovieEdit="+id,true); //servlet name
        xmlhttpmovie.onreadystatechange  = handleServerResponseForGotoEditMovie;
        xmlhttpmovie.send();
    }
   
    
    
}

function handleServerResponseForGotoEditMovie(){
      
     if (xmlhttpmovie.readyState==4 && xmlhttpmovie.status==200)
     {
            document.getElementById("realbody").innerHTML=xmlhttpmovie.responseText;
     }
    else if(xmlhttpmovie.readyState==4 && xmlhttpmovie.status==404){
            xmlhttpmovie.getElementById("realbody").innerHTML="<h3>Page Not Found. Please Try Again</h3>";
     }
}


function movieEdit(id, pic, name, description, releasedate, idcategory, isactive){
      
     
     var moviename;
     var moviepic;
     var moviedesc;
     var movierelease;
     var moviecategoryid = document.getElementById("moviecategoryselect").options[document.getElementById("moviecategoryselect").selectedIndex].value;
     var movieprice = document.getElementById("editmovieprice").value;
     
        
     
     
      try{
          moviepic = document.getElementById("specificmoviepic").name;
      }catch(error){
          moviepic = pic;
          
          
      }
      
       
      try{
          moviename = document.getElementById("editmoviename").value;
      }catch(error){
          moviename = name;
          
      }
      
      try{
          moviedesc = document.getElementById("editmoviedescription").value;
      }catch(error){
          moviedesc = description;
      }
      
      try{
          moviedesc = document.getElementById("editmoviedescription").value;
      }catch(error){
          moviedesc = description;
      }
      
      try{
          movierelease = document.getElementById("editmoviereleasedate").value;
      }catch(error){
          movierelease = releasedate;
      }
      
     
   
        
    
      if(xmlhttpmovie) {
        document.getElementById("submiteditmovie").innerHTML="<div style='text-align: left;'><img src='../images/loader.gif' alt='Loading' align='left' width='50px'  /></div>";
        xmlhttpmovie.open("GET","../AdminEditMovie?id="+id+"&pic="+moviepic+"&name="+moviename+"&desc="+moviedesc+"&release="+movierelease+"&idcategory="+moviecategoryid+"&price="+movieprice+"&isactive="+isactive,true); //servlet name
        xmlhttpmovie.onreadystatechange  = handleServerResponseForEditMovie;
        xmlhttpmovie.send();
      }
   
    
    
}

function handleServerResponseForEditMovie(){
          if (xmlhttpmovie.readyState==4 && xmlhttpmovie.status==200)
     {
            document.getElementById("realbody").innerHTML=xmlhttpmovie.responseText;
     }
    else if(xmlhttpmovie.readyState==4 && xmlhttpmovie.status==404){
            xmlhttpmovie.getElementById("realbody").innerHTML="<h3>Page Not Found. Please Try Again</h3>";
     }
}

function movieAdd(){
      
     var date = new Date();
     var month = date.getMonth()+1;
     var moviename = document.getElementById("inputmoviename").value;
     var moviepic = document.getElementById("inputmoviepic").value.substr(12);
     var moviedesc = document.getElementById("inputmoviedesc").value; 
     var moviecategoryid = document.getElementById("moviecategoryselect").options[document.getElementById("moviecategoryselect").selectedIndex].value;
     var movierelease = document.getElementById("inputmoviereleasedate").value;
     var movieprice = document.getElementById("inputmovieprice").value;
     var moviecreatedate = date.getFullYear() +"-"+month+"-"+date.getDate();
     var moviecreatetime = date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
     
     
     if(!moviename || !moviedesc || !movierelease){
         document.getElementById("addmovieerror").innerHTML = "<font color='red'>You must fill the name and description</font>";
     }
     else{
      if(confirm("Are you sure?") == true){   
     
      if(xmlhttpmovie) {
        document.getElementById("addmovieerror").innerHTML = "";  
        document.getElementById("submitaddmovie").innerHTML="<div style='text-align: left;'><img src='../images/loader.gif' alt='Loading' align='left' width='50px'  /></div>";
        xmlhttpmovie.open("GET","../AdminAddMovie?pic="+moviepic+"&name="+moviename+"&desc="+moviedesc+"&release="+movierelease+"&price="+movieprice+"&idcategory="+moviecategoryid+"&createdate="+moviecreatedate+"&createtime="+moviecreatetime,true); //servlet name
        xmlhttpmovie.onreadystatechange  = handleServerResponseForEditMovie;
        xmlhttpmovie.send();
      }
      }
   
     }
    
}

function deleteMovie(id){
      
      if(confirm("Are you sure?") == true){   
     
      if(xmlhttpmovie) {
        document.getElementById("deletemovie"+id).innerHTML="<div style='text-align: left;'><img src='../images/loader.gif' alt='Loading' align='left' width='50px'  /></div>";
        xmlhttpmovie.open("GET","../AdminDeleteMovie?id="+id,true); //servlet name
        xmlhttpmovie.onreadystatechange  = handleServerResponseForGotoEditMovie;
        xmlhttpmovie.send();
      }
      
   
     }
    
}

function restoreMovie(id){
      
      if(confirm("Are you sure?") == true){   
     
      if(xmlhttpmovie) {
        document.getElementById("restoremovie"+id).innerHTML="<div style='text-align: left;'><img src='../images/loader.gif' alt='Loading' align='left' width='50px'  /></div>";
        xmlhttpmovie.open("GET","../AdminRestoreMovie?id="+id,true); //servlet name
        xmlhttpmovie.onreadystatechange  = handleServerResponseForGotoEditMovie;
        xmlhttpmovie.send();
      }
      
   
     }
    
}

function addStocktoMovie(id){
      var stockvalue = document.getElementById("stockinput").value;
      
      if(stockvalue!=0 && confirm("Are you sure?") == true){  
      
      if(xmlhttpmovie) {
        document.getElementById("addstock"+id).innerHTML="<div style='text-align: auto;'><img src='../images/loader.gif' alt='Loading' align='left' width='50px'  /></div>";
        xmlhttpmovie.open("GET","../AdminAddStockToMovie?id="+id+"&quantity="+stockvalue,true); //servlet name
        xmlhttpmovie.onreadystatechange  = function(){
            
            if (xmlhttpmovie.readyState==4 && xmlhttpmovie.status==200)
            {
                document.getElementById("realbody").innerHTML = xmlhttpmovie.responseText;
            }
            else if(xmlhttpmovie.readyState==4 && xmlhttpmovie.status==404){
                xmlhttpmovie.getElementById("realbody").innerHTML="<h3>Page Not Found. Please Try Again</h3>";
            }
        };
        xmlhttpmovie.send();
      }
      }
      
}  
    


     
     
     















